import {
	getUserInfoApi
} from "@/api/user";
import {
	defineStore
} from "pinia";

const defaultData = {
	id: -1,
	nickname: '',
	avatar: '',
	phonenumber: ''
}

export const useUserStore = defineStore('user', {
	state: () => {
		return {
			token: '',
			userInfo: defaultData
		}
	},
	actions: {
		clearLogin() {
			this.token = '';
			this.userInfo = {
				id: -1,
				nickname: '',
				avatar: '',
				phonenumber: ''
			}
		},

		async getUserInfo() {
			try {
				const results = await getUserInfoApi({
					uid: this.userInfo.id
				});
				console.log(results,"resultsresults")
				Object.assign(this.userInfo, results);
			} catch (error) {
				console.error('getUserInfo error', error);
				throw new Error(error);
			}
		}
	},
	persist: {
		enabled: true,
		strategies: [{
			key: 'userStore',
			storage: localStorage,
			paths: ['token', 'userInfo']
		}]
	}
});